<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Http\Requests\Admin\BaseRequest;
use App\Models\Admin\AdminProduct;
use App\Models\Admin\CompanyType;
use App\Models\Admin\CustomerServiceRole;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class ProductController extends AdminController
{
    use AdminTrait;

    protected $model;
    protected $unique_id = '';
    public function __construct(AdminProduct $model)
    {
        parent::__construct();
        $this->model = $model;
    }
    /**
     * 定义验证规则
     * @var []
     */
    protected $validate_rule = [
    ];

    /**
     * 定特殊的验证规则
     */
    protected function rules()
    {
        return [
            'name'     => [
                'required',
//                Rule::unique('admin_product')->ignore($this->unique_id),
            ],
            'company_type'=>[
                'required',
//                'integer',
//                Rule::exists('company_type','id'),
            ],
            'id'=>[
                'nullable',
                Rule::exists('admin_product','id'),
            ]
        ];
    }

    protected $err_msg = [
        'name.unique'         => '名称已存在',
        'id.exists'         => 'id不存在',
    ];

    protected function search($request){
        return $this->model;
    }

    protected function beforeIndex()
    {
        $company_type_list=CompanyType::pluck('name','id');
        return [
            'company_type_list'=>$company_type_list
        ];

    }

    protected function setRequestField($arr,$type=false){
        //过滤字段
        if(isset($arr['_token'])) unset($arr['_token']);
        if (isset($arr['sign'])) unset($arr['sign']);
        if (isset($arr['timeStamp'])) unset($arr['timeStamp']);
        return $arr;
    }

    public function getCustRoleList(BaseRequest $request)
    {
        $list=CustomerServiceRole::get();
        return $this->success(['list'=>$list]);
    }

    public function  addCustRole(BaseRequest $request)
    {
        $input=$request->only('name','remark');
        $rules=[
            'name'=>"required|unique:customer_service_role,name",
        ];
        $validator = Validator::make($input,$rules,$this->err_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        DB::beginTransaction();
        try {
            $info=CustomerServiceRole::create($input);
            CustomerServiceRole::addLog($info->toArray(),[],1);
            DB::commit();
            return $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return $this->error(['-1',$exception->getMessage()]);
        }
    }

    public function  editCustRole(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'id'=>'required|exists:customer_service_role,id',
            'name'=>[
              'required',
              Rule::unique('customer_service_role','name')->ignore($input['id']??0)
            ],
        ];
        $validator = Validator::make($input,$rules,$this->err_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        DB::beginTransaction();
        try {
            $info=CustomerServiceRole::find($input['id']);
            $perInfo = $info->getOriginal();//获取修改之前的字段
            $info->name=$input['name'];
            $info->remark=$input['remark']??'';
            $info->save();
            $nextInfo = $info->getChanges();//获取修改之后的字段
            CustomerServiceRole::addLog($perInfo,$nextInfo,2);
            DB::commit();
            return $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return $this->error();
        }
    }
}

